package com.skyline.wo.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.skyline.base.dao.impl.BaseDaoImpl;
import com.skyline.base.type.Activity;
import com.skyline.base.type.Authority;
import com.skyline.common.cache.annotation.Cache;
import com.skyline.common.cache.annotation.CacheCategoryType;
import com.skyline.common.cache.annotation.CacheDelete;
import com.skyline.common.cache.annotation.Fk;
import com.skyline.common.cache.annotation.Param;
import com.skyline.wo.dao.CategoryDao;
import com.skyline.wo.mapper.CategoryMapper;
import com.skyline.wo.model.Category;

/**
 * CategoryDaoImpl is the implement of the Category DATABASE ACCESS operation
 * 
 * @author Jairus Chan
 * @version 0.1
 * 
 */
@Repository("categoryDao")
public class CategoryDaoImpl extends BaseDaoImpl implements CategoryDao {

	@Value("${CategoryDao.inseartCategory}")
	private String inseartCategorySql;

	@Value("${CategoryDao.queryCategoryByOwnerId}")
	private String queryCategoryByOwnerIdSql;

	@Value("${CategoryDao.updateCategory}")
	private String updateCategorySql;

	@Value("${CategoryDao.deleteCategory}")
	private String deleteCategorySql;

	@Value("${CategoryDao.countSameCategory}")
	private String countSameCategorySql;

	@Value("${CategoryDao.updateArticleNumAdd}")
	private String updateArticleNumAddSql;

	@Value("${CategoryDao.updateArticleNumMinus}")
	private String updateArticleNumMinusSql;

	@Value("${CategoryDao.queryCategoryById}")
	private String queryCategoryByIdSql;
	
	@Value("${CategoryDao.updateArticles}")
	private String updateArticlesSql;

	@Override
	@CacheDelete(type=CacheCategoryType.CATEGORY)
	public void inseartCategory(@Fk Long ownerId, String ownerPortrait, String ownerNickname, String categoryName, Activity activity) {
		jdbcTemplate.update(inseartCategorySql,Authority.PUBLIC, ownerId, ownerPortrait, ownerNickname, activity.toString(), categoryName);
	}

	@Override
	@CacheDelete(type=CacheCategoryType.CATEGORY)
	public void updateCategory(Long id, String categoryName, @Fk Long ownerId) {
		jdbcTemplate.update(updateCategorySql, categoryName, id, ownerId);
		//TODO:可能会存在数据不一致，到时请修改
		jdbcTemplate.update(updateArticlesSql, categoryName, id, ownerId);
	}

	@Override
	@CacheDelete(type=CacheCategoryType.CATEGORY)
	public void updateArticleNumAdd(Long id,@Fk Long ownerId, Integer num) {
		jdbcTemplate.update(updateArticleNumAddSql, num, id, ownerId);
	}

	@Override
	@CacheDelete(type=CacheCategoryType.CATEGORY)
	public void updateArticleNumMinus(Long id, @Fk Long ownerId, Integer num) {
		jdbcTemplate.update(updateArticleNumMinusSql, num, id, ownerId);
	}

	@Override
	@CacheDelete(type=CacheCategoryType.CATEGORY)
	public void deleteCategory(Long id,@Fk Long ownerId) {
		jdbcTemplate.update(deleteCategorySql, id,ownerId);
	}

	@Override
	@Cache(keyPattern = "category-l-:uid", type = CacheCategoryType.CATEGORY)
	public List<Category> queryCategoryByOwnerId(@Param("uid") Long ownerId) {

		return jdbcTemplate.query(queryCategoryByOwnerIdSql, CategoryMapper.getMapper(), ownerId, Activity.NORMAL.toString());

	}

	@Override
	@Cache(keyPattern = "category-i-:uid-:categoryName", type = CacheCategoryType.CATEGORY)
	public Integer countSameCategory(@Param("uid") Long ownerId,@Param("categoryName") String categoryName) {
		return jdbcTemplate.queryForInt(countSameCategorySql, ownerId, categoryName, Activity.NORMAL.toString());
	}

	@Override
	@Cache(keyPattern = "category-:id", type = CacheCategoryType.CATEGORY)
	public Category queryCategoryById(@Param("id") Long id) {
		return jdbcTemplate.queryForObject(queryCategoryByIdSql, CategoryMapper.getMapper(), id, Activity.NORMAL.toString());
	}

}
